<?php
/**
 * @category   Oos
 * @package    Oos_Site
 * @license    New BSD License
 */

/**
 * @see Zend_Gdata_EntryAtom
 */
require_once 'Zend/Gdata/Entry.php';

/**
 * @see 
 */
require_once 'Oos/Site/Extension/SubDependentLocality.php';
require_once 'Oos/Site/Extension/DependentLocality.php';
require_once 'Oos/Site/Extension/Locality.php';
require_once 'Oos/Site/Extension/SubAdministrativeArea.php';
require_once 'Oos/Site/Extension/AdministrativeArea.php';
require_once 'Oos/Site/Extension/Country.php';
require_once 'Oos/Site/Extension/UserAddress.php';
require_once 'Oos/Site/Extension/Phone.php';
require_once 'Oos/Site/Extension/OosId.php';
require_once 'Oos/Site/Extension/GeoRSS.php';
require_once 'Oos/Site/Extension/Point.php';
require_once 'Oos/Site/Extension/Pos.php';




class Oos_Site_SiteEntry extends Zend_Gdata_Entry
{

    protected $_entryClassName = 'Oos_Site_Entry';
    protected $_locality = null;
    protected $_subdependentlocality = null;
    protected $_dependentlocality = null;
    protected $_subadministrativearea = null;
    protected $_administrativearea = null;
    protected $_country = null;
    protected $_oosid = null;
    protected $_useraddress = null;
    protected $_phone = null;
	protected $_georss = null;
	
	protected $_tagscheme;
	protected $_listscheme;
	
    public function __construct($element = null)
    {
        foreach (Oos_Site::$namespaces as $nsPrefix => $nsUri) {
            $this->registerNamespace($nsPrefix, $nsUri);
        }
        parent::__construct($element);
        
        $this->_id = new Zend_Gdata_App_Extension_Id('http://localhost/');
    }

    public function getDOM($doc = null)
    {
        $element = parent::getDOM($doc);
        if ($this->_locality != null) {
            $element->appendChild($this->_locality->getDOM($element->ownerDocument));
        }
        if ($this->_subdependentlocality != null) {
            $element->appendChild($this->_subdependentlocality->getDOM($element->ownerDocument));
        }
        if ($this->_dependentlocality != null) {
            $element->appendChild($this->_dependentlocality->getDOM($element->ownerDocument));
        }
        if ($this->_subadministrativearea != null) {
            $element->appendChild($this->_subadministrativearea->getDOM($element->ownerDocument));
        }
        if ($this->_administrativearea != null) {
            $element->appendChild($this->_administrativearea->getDOM($element->ownerDocument));
        }
        if ($this->_country != null) {
            $element->appendChild($this->_country->getDOM($element->ownerDocument));
        }
        if ($this->_useraddress != null) {
            $element->appendChild($this->_useraddress->getDOM($element->ownerDocument));
        }
        if ($this->_oosid != null) {
            $element->appendChild($this->_oosid->getDOM($element->ownerDocument));
        }
        if ($this->_georss != null) {
        	$element->appendChild($this->_georss->getDOM($element->ownerDocument));
        }
        if ($this->_phone != null) {
            $element->appendChild($this->_phone->getDOM($element->ownerDocument));
        }

        return $element;
    }

    protected function takeChildFromDOM($child)
    {
        $absoluteNodeName = $child->namespaceURI . ':' . $child->localName;

        switch ($absoluteNodeName) {
            case $this->lookupNamespace('oos') . ':' . 'locality';
  	        $locality = new Oos_Site_Extension_Locality();
                $locality->transferFromDOM($child);
                $this->_locality = $locality;
                break; 
            case $this->lookupNamespace('oos') . ':' . 'subdependentlocality';
  	        $sdlocality = new Oos_Site_Extension_SubDependentLocality();
                $sdlocality->transferFromDOM($child);
                $this->_subdependentlocality = $sdlocality;
                break;
            case $this->lookupNamespace('oos') . ':' . 'dependentlocality';
  	        $dlocality = new Oos_Site_Extension_DependentLocality();
                $dlocality->transferFromDOM($child);
                $this->_dependentlocality = $dlocality;
                break;
            case $this->lookupNamespace('oos') . ':' . 'subadministrativearea';
  	        $saa = new Oos_Site_Extension_SubAdministrativeArea();
                $saa->transferFromDOM($child);
                $this->_subadministrativearea = $saa;
                break;
            case $this->lookupNamespace('oos') . ':' . 'administrativearea';
  	        $aa = new Oos_Site_Extension_AdministrativeArea();
                $aa->transferFromDOM($child);
                $this->_administrativearea = $aa;
                break;
            case $this->lookupNamespace('oos') . ':' . 'country';
  	        $country = new Oos_Site_Extension_Country();
                $country->transferFromDOM($child);
                $this->_country = $country;
                break; 
            case $this->lookupNamespace('oos') . ':' . 'useraddress';
                $ua = new Oos_Site_Extension_UserAddress();
                $ua->transferFromDOM($child);
                $this->_useraddress = $ua;
                break; 
            case $this->lookupNamespace('oos') . ':' . 'id';
                $id = new Oos_Site_Extension_OosId();
                $id->transferFromDOM($child);
                $this->_oosid = $id;
                break;
            case $this->lookupNamespace('oos') . ':' . 'telephone';
                $phone = new Oos_Site_Extension_Phone();
                $phone->transferFromDOM($child);
                $this->_phone = $phone;
                break;
            case $this->lookupNamespace('georss') . ':' . 'where';
                $point = new Oos_Site_Extension_GeoRSS();
                $point->transferFromDOM($child);
                $this->_georss = $point;
                break;                
            default:
                parent::takeChildFromDOM($child);
                break;
        }
    }

	public function setUserAddress($value)
	{
		$this->_useraddress = new Oos_Site_Extension_UserAddress($value);
	}
	
	public function getUserAddress()
	{
		if ($this->_useraddress != null)
			return $this->_useraddress->text;
		
		return '';
	}
	

	public function setOosid($value)
	{
		$this->_oosid = new Oos_Site_Extension_OosId($value);
	}
	
	public function getOosid()
	{
		return $this->_oosid->text;
	}
	
	public function setLocality($value, $slug)
	{
		$this->_locality = new Oos_Site_Extension_Locality($value, $slug);
	}
	
	public function setCountry($value, $slug)
	{
		$this->_country = new Oos_Site_Extension_Country($value, $slug);
	}

	public function getCountry()
	{
		return $this->_country->getAsArray();
	}
	
	public function getLocality()
	{
		return $this->_locality->getAsArray();
	}
	
	public function setPhone($value) {
		$this->_phone = new Oos_Site_Extension_Phone($value);
	}
	
	public function getPhone() {
		if ($this->_phone != null)
			return $this->_phone->text;
			
		return '';
	}
	
	public function setCoordinates($value) {
		$pos = new Oos_Site_Extension_Pos($value);
		$point = new Oos_Site_Extension_Point($pos);
		$georss = new Oos_Site_Extension_GeoRSS();
		$georss->point = $point;
		$this->_georss = $georss;
	}

	public function getCoordinates() {
		return $this->_georss->point->pos->text;
	}
	
	public function setName($value) {
		$this->_title = new Zend_Gdata_App_Extension_Title($value);	
	}

	public function getName() {
		return $this->_title->text;	
	}

	public function setReviewTitle($value) {
		$this->_summary = new Zend_Gdata_App_Extension_Summary($value);
	}
	
	public function setReviewContent($value) {
		$this->_content = new Zend_Gdata_App_Extension_Content($value);		
	}
	
	public function getReviewTitle() {
		return $this->_summary;
	}
	
	public function getReviewContent() {
		return $this->_content;
	}
	
	public function setPrivacy($value) {
      $cat = new Zend_Gdata_App_Extension_Category();
      $cat->setTerm($value);
      $cat->setScheme('http://localhost/api/v1/privacy');
    
      $this->_category[] = $cat;
	}
	
	public function getPrivacy() {
      foreach ($this->_category as $elem) {
      	if ($elem->getScheme() == 'http://localhost/api/v1/privacy')
      		return $elem->getTerm();
      }    		
	}

	public function getTags() {
      $tags = array();
      foreach ($this->_category as $elem) {
      	if ($elem->getScheme() == $this->_tagscheme)
      		$tags[] = $elem->getTerm();
      }
      return $tags;
    }

    public function addTag($value) {
      $cat = new Zend_Gdata_App_Extension_Category();
  	  $cat->setTerm($value);
  	  $cat->setScheme($this->_tagscheme);
	  $this->_category[] = $cat;	
    }

    public function getLists() {
      $lists = array();
      foreach ($this->_category as $elem) {
      	if ($elem->getScheme() == $this->_listscheme)
      		$lists[] = $elem->getTerm();
      }
      return $lists;
    }

    public function addList($value) {
      $cat = new Zend_Gdata_App_Extension_Category();
  	  $cat->setTerm($value);
  	  $cat->setScheme($this->_listscheme);
	  $this->_category[] = $cat;	
    }

    public function setTagScheme($value) {
    	$this->_tagscheme = $value;
    }
    
    public function getTagScheme() {
    	return $this->_tagscheme;
    }
    
    public function setListScheme($value) {
    	$this->_listscheme = $value;
    }
    
    public function setAuthorName($value) {
    	$this->_author[] = new Zend_Gdata_App_Extension_Author(new Zend_Gdata_App_Extension_Name($value));
    }
    
    public function validate() {
    	if (!$this->_author) {
    		$this->_author[] = new Zend_Gdata_App_Extension_Author(new Zend_Gdata_App_Extension_Name('none'));	
    	}
    	$this->_updated = new Zend_Gdata_App_Extension_Updated(gmdate('Y-m-d\TH:i:s\Z'));
    }
}

?>